package leetcode;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Stack;

public class LeetCode145 {
    //前序 后序是类似的
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> result = new ArrayList<>();
        Stack<TreeNode> stack = new Stack<>();
        if (root == null) {
            return result;
        }
        TreeNode current = root;
        stack.push(root);
        while (current != null && !stack.isEmpty()) {
            current = stack.pop();
            result.add(current.val);
            if (current.left != null)
                stack.push(current.left);
            if (current.right != null)
                stack.push(current.right);
        }
        Collections.reverse(result);
        return result;
    }
//------------------------------------------------------------------
}
